﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Mimoza.Common;

namespace Mimoza.ObjectLayer
{
    public class HostRepository
    {
        private OL ol;
        public HostRepository(OL ol)
        {
            this.ol = ol;
            this.ol.DBLayer.DBHostRepository.OnObjectChanged += DBObjectChangedEventHandler;
        }

        public event EventHandler<ObjectLayer_ObjectChangedEventArgs> OnObjectChanged;
        private void RaiseOnObjectChanged(ObjectLayer_ObjectChangedEventArgs e)
        {
            EventHandler<ObjectLayer_ObjectChangedEventArgs> handler = OnObjectChanged;

            if (handler != null)
            {
                handler(this, e);
            }
        }
        void DBObjectChangedEventHandler(object sender, DBObjectChangedEventArgs e)
        {
            ObjectLayerEventType type = ObjectLayerEventType.ObjectInserted;
            Object obj;
            switch ( e.EventType )
            {
                case DBEventType.ObjectInserted:
                    type = ObjectLayerEventType.ObjectInserted;
                    break;
                case DBEventType.ObjectUpdated:
                    type = ObjectLayerEventType.ObjectUpdated;
                    break;
                case DBEventType.ObjectDeleted:
                    type = ObjectLayerEventType.ObjectDeleted;
                    break;
            }

            obj = this.CreateHost(e.Object as HostRecord);
            ObjectLayer_ObjectChangedEventArgs args = new ObjectLayer_ObjectChangedEventArgs(obj,type);

            RaiseOnObjectChanged(args);
        }

        public Host CreateHost()
        {
            return new Host(ol);
        }
        public Host CreateHost(Guid HostID)
        {
            HostRecord record = new HostRecord();
            record.ID = HostID;
            return new Host(ol, record);
        }
        public Host CreateHost(HostRecord record)
        {
            return new Host(ol, record);
        }

        public Host GetHostByID(Guid ID)
        {
            HostRecord hr = ol.DBLayer.DBHostRepository.GetHost(ID);
            if (hr == null) return null;
            return new Host(ol, hr);
        }
        public Host GetHostByMAC(string MACAddress)
        {
            HostRecord hr = ol.DBLayer.DBHostRepository.GetHost(MACAddress);
            if (hr == null) return null;
            return new Host(ol, hr);
        }
        public Host GetHostByName(string hostName)
        {
            HostRecord hr = ol.DBLayer.DBHostRepository.GetHostByName(hostName);
            if (hr == null) return null;
            return new Host(ol, hr);
        }
        public void Add(Host host)
        {
            ol.DBLayer.DBHostRepository.Insert(host.Record);
        }
        public void Delete(Host host)
        {
            ol.DBLayer.DBHostRepository.Delete(host.Record);
        }
        public void Update(Host host)
        {
            ol.DBLayer.DBHostRepository.Update(host.Record);
        }
    }
}
